var types = ['default', 'primary', 'warn']
var pageObject = {
    data: {
        defaultSize: 'default',
        primarySize: 'default',
        warnSize: 'default',
        disabled: false,
        plain: false,
        loading: false
    },
    setDisabled: function (e) {
        this.setData({
            disabled: !this.data.disabled
        })
    },
    setPlain: function (e) {
        this.setData({
            plain: !this.data.plain
        })
    },
    setLoading: function (e) {
        this.setData({
            loading: !this.data.loading
        })
    },
    openBluetoothAdapter: function (e) {
        console.log('openBluetoothAdapter click');
        wx.openBluetoothAdapter({
            success: function (res) {
                // success
                console.log(res)
            },
            fail: function (res) {
                // fail
            },
            complete: function (res) {
                // complete
            }
        })
    },
    closeBluetoothAdapter: function (e) {
        wx.closeBluetoothAdapter({
            success: function (res) {
                // success
                console.log('success ' + res)
            },
            fail: function (res) {
                // fail
                console.log('fail ' + res)
            },
            complete: function (res) {
                // complete
                console.log('complete ' + res)
            }
        })
    },
    getBluetoothAdapterState: function (e) {
        wx.getBluetoothAdapterState({
            success: function (res) {
                // success
                console.log('success ' + res.errMsg)
            },
            fail: function (res) {
                // fail
                console.log('fail ' + res.errMsg)
            },
            complete: function (res) {
                // complete
                console.log('complete ' + res.errMsg)
            }
        })
    },
    onBluetoothAdapterStateChange: function (e) {
        wx.onBluetoothAdapterStateChange(function (res) {
            // callback
            console.log('discovering: ', res.discovering);

        })
    },
    startBluetoothDevicesDiscovery: function (e) {
        wx.startBluetoothDevicesDiscovery({
            services: ['CC03'],
            success: function (res) {
                // success
                console.log('success ' + res.errMsg)
                wx.showToast({
                    title: res.errMsg,
                    icon: "loading",
                    duration: 5000
                })
            },
            fail: function (res) {
                // fail
                console.log('fail ' + res.errMsg)

                wx.showToast({
                    title: res.errMsg,
                    icon: "loading",
                    duration: 5000
                })
            },
            complete: function (res) {
                // complete
                console.log('complete ' + res.errMsg)
                wx.showToast({
                    title: res.errMsg,
                    icon: "loading",
                    duration: 5000
                })
            }
        })
    },
    stopBluetoothDevicesDiscovery: function (e) {
        wx.stopBluetoothDevicesDiscovery({
            success: function (res) {
                // success
                console.log('success ' + res.errMsg)
            },
            fail: function (res) {
                // fail
                console.log('fail ' + res.errMsg)
            },
            complete: function (res) {
                // complete
                console.log('complete ' + res.errMsg)
            }
        })
    },
    getBluethoothDevices: function (e) {
        wx.getBluetoothDevices({
            success: function (res) {
                // success
                console.log('success ' + res.errMsg)
                var devNum = 0;
                for (var dev in res.devices) {
                    console.log('dev ' + devNum, dev.deviceId + ' ' + dev.name + ' ' + dev.RSSI);
                    ++devNum;
                }
            },
            fail: function (res) {
                // fail
                console.log('fail ' + res.errMsg)
            },
            complete: function (res) {
                // complete
            }
        })
    },
    onBluetoothDeviceFound: function (e) {
        wx.onBluetoothDeviceFound(function (res) {
            // callback
            console.log('onBluetoothDeviceFound ' + res.devices.length)
            for (var dev in res.devices) {
                console.log('dev: ', dev.deviceId + ' ' + dev.name);
            }
        })
    }

}

for (var i = 0; i < types.length; ++i) {
    (function (type) {
        pageObject[type] = function (e) {
            var key = type + 'Size'
            var changedData = {}
            changedData[key] =
                this.data[key] === 'default' ? 'mini' : 'default'
            this.setData(changedData)
        }
    })(types[i])
}

Page(pageObject)
